package com.administration.controller;


import com.administration.constants.Result;
import com.administration.dao.SysDeptMapper;
import com.administration.dao.SysUserMapper;
import com.administration.dao.SysUserPostMapper;
import com.administration.dao.SysUserRoleMapper;
import com.administration.pojo.SysDept;
import com.administration.pojo.SysUser;
import com.administration.pojo.SysUserPost;
import com.administration.pojo.SysUserRole;
import com.administration.utils.PasswordUtils;
import com.administration.utils.ResultData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

@Controller
@RequestMapping("/sysUser")
public class SysUserController {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysDeptMapper sysDeptMapper;

    @Resource
    private SysUserPostMapper sysUserPostMapper;

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    //用户主界面
    @RequestMapping("/user.do")
    public String UserIndex(){
        return "/static/user/layui_user_index.html";
    }

    //查询所有用户信息/查询单个
    @ResponseBody
    @RequestMapping("/selectAllUser.do")
    public ResultData selectAllUser(SysUser sysUser,String startTime,String endTime) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", sysUser.getUserId());
        map.put("loginName",sysUser.getLoginName());
        map.put("phoneNumber",sysUser.getPhoneNumber());
        map.put("status",sysUser.getStatus());

        //用于日期判断
        StringBuffer strTime = new StringBuffer();

        List<SysUser> list = sysUserMapper.selectUserList(map);
        List<SysUser> lists = new ArrayList<>();
        for (SysUser su:list){
            //修改状态显示
            if ("0".equals(su.getStatus())){
                su.setStatus("正常");
            }else {
                su.setStatus("停用");
            }
            //修改职位显示
            SysDept sysDept = sysDeptMapper.selectDeptById(su.getDeptId());
            su.setDeptName(sysDept.getDeptName());

            //对查询日期进行判断，值为空不进行判断
            if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
                //获取查询日期并进行转换
                //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                //String searchStartTime = sdf.format(startTime);
                //String searchEndTime = sdf.format(endTime);

                //日期判断添加结束时间结束值
                if(strTime.length() <= 0){
                    strTime.append(endTime);
                    strTime.append(" 59:59:59");
                    endTime = strTime.toString();
                }

                //日期搜索判断
                String createTime = su.getCreateTime();
                if (createTime.compareTo(startTime) >= 0 && createTime.compareTo(endTime) <= 0){
                    lists.add(su);
                }
            }else {
                lists.add(su);
            }
        }

        return new ResultData<>(Result.SUCCESS.getCode(), "", lists.size(), lists);
    }

    //新增用户信息
    @ResponseBody
    @RequestMapping("/addUser.do")
    public int addUser(SysUser sysUser, SysUserRole sysUserRole, SysUserPost sysUserPost) {
        //密码加盐加密
        String salt = PasswordUtils.getSalt();
        sysUser.setSalt(salt);
        String endPassword = PasswordUtils.encode(sysUser.getPassword(),salt);
        sysUser.setPassword(endPassword);

        //用户添加
        int addRes = sysUserMapper.addUser(sysUser);

        //对角色关系表和岗位关系表进行关联添加
        Map<String, Object> map = new HashMap<>();
        map.put("loginName", sysUser.getLoginName());
        List<SysUser> list = sysUserMapper.selectUserList(map);
        for (SysUser su:list){
            sysUserPost.setUserId(su.getUserId());
            sysUserRole.setUserId(su.getUserId());
        }
        int addUserRoleResult = sysUserRoleMapper.addUserRole(sysUserRole);
        int addUserPostResult = sysUserPostMapper.addUserPost(sysUserPost);

        return addRes;
    }

    //更新用户信息
    @ResponseBody
    @RequestMapping("/updateUser.do")
    public int updateUser(SysUser sysUser, String oldPassword, String newPassword, String newPasswordTrue, SysUserRole sysUserRole, SysUserPost sysUserPost) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", sysUser.getUserId());
        List<SysUser> list = sysUserMapper.selectUserList(map);
        //密码判断
        for (SysUser su : list) {
            if (!"".equals(oldPassword) && oldPassword != null) {
                //密码加盐判断
                if (PasswordUtils.matches(su.getSalt(),oldPassword,su.getPassword())) {
                    if (!"".equals(newPassword) && newPassword != null) {
                        if (!"".equals(newPasswordTrue) && newPasswordTrue != null) {
                            //新密码和确认密码进行匹配判断
                            if (newPassword.equals(newPasswordTrue)) {
                                //密码加盐加密
                                String endPassword = PasswordUtils.encode(newPassword,su.getSalt());
                                sysUser.setPassword(endPassword);
                            } else {
                                return 5;
                            }
                        } else {
                            return 4;
                        }
                    } else {
                        return 3;
                    }

                } else {
                    return 2;
                }
            }
        }

        //对角色关系表和岗位关系表进行关联修改
        if (sysUserPost.getPostId() != null && !"".equals(sysUserPost.getPostId())
        && sysUserRole.getRoleId() != null && !"".equals(sysUserRole.getRoleId())){
            sysUserPost.setUserId(sysUser.getUserId());
            sysUserRole.setUserId(sysUser.getUserId());
            int updateUserRoleResult = sysUserRoleMapper.updateUserRole(sysUserRole);
            int updateUserPostResult = sysUserPostMapper.updateUserPost(sysUserPost);
        }

        //用户信息修改
        int updateRes = sysUserMapper.updateUser(sysUser);
        return updateRes;
    }

    //删除用户信息
    @ResponseBody
    @RequestMapping("/deleteUser.do")
    public int deleteUser(SysUser sysUser) {
        int deleteRes = sysUserMapper.deleteUser(sysUser.getUserId());
        SysUserPost sysUserPost = new SysUserPost();
        sysUserPost.setUserId(sysUser.getUserId());

        //对角色关系表和岗位关系表进行关联删除
        int deleteUserRoleResult = sysUserRoleMapper.deleteUserRole(sysUser.getUserId());
        int deleteUserPostResult = sysUserPostMapper.deleteUserPost(sysUserPost);
        return deleteRes;
    }

    //查询所有用户角色信息用于显示
    @ResponseBody
    @RequestMapping("/selectUserRole.do")
    public ResultData selectUserRole(SysUserRole sysUserRole){
        List<SysUserRole> list = sysUserRoleMapper.selectAllUserRole(sysUserRole);
        return new ResultData<>(Result.SUCCESS.getCode(), "", list.size(), list);
    }

    //查询所有用户岗位信息用于显示
    @ResponseBody
    @RequestMapping("/selectUserPost.do")
    public ResultData selectUserPost(SysUserPost sysUserPost){
        List<SysUserPost> list = sysUserPostMapper.selectAllUserPost(sysUserPost);
        return new ResultData<>(Result.SUCCESS.getCode(), "", list.size(), list);
    }
}
